/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.ejb.database;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Administrator
 */
@Stateless
@EJB(name="java:global/UsersFacade", beanInterface=UsersFacadeLocal.class)
public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeLocal {
    @PersistenceContext(unitName = "MDC-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersFacade() {
        super(Users.class);
    }
    
    @Override
    public Users getUserByUsername(String username) {
        try {
            Users user = (Users)getEntityManager().createNamedQuery("Users.findByUsername").
                    setParameter("username", username).getSingleResult();
            return user;
        } catch (Exception nre) {
            //l'utente non è presente nel database e può essere inserito
            return null;
        }
    }
    
    @Override
    public void create(Users user)
    {
        user.setPoint(0);
        getEntityManager().persist(user);
    }
}
